package com.xdqzy.baseline.demo.controller;


import com.xdqzy.baseline.common.annotation.LogTag;
import com.xdqzy.baseline.common.exception.CtbcAssert;
import com.xdqzy.baseline.common.manager.RedisCacheManager;
import com.xdqzy.baseline.common.manager.StaticDataManager;
import com.xdqzy.baseline.common.model.RestResult;
import com.xdqzy.baseline.demo.constant.ProductConstants;
import com.xdqzy.baseline.demo.controller.param.req.AddProductReq;
import com.xdqzy.baseline.demo.controller.param.req.ProductIdReq;
import com.xdqzy.baseline.demo.controller.param.resp.ProductDemoResp;
import com.xdqzy.baseline.demo.model.ProductDemoModel;
import com.xdqzy.baseline.demo.service.ProductDemoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author qss
 * @since 2020-11-15
 */
@Api(tags = "商品接口")
@RestController
@RequestMapping("/api/v1/productDemo")
public class ProductDemoController {

    @Resource
    private ProductDemoService productDemoService;


    @Resource
    private RedisCacheManager redisCacheManager;

    @Resource
    private StaticDataManager staticDataManager;

    @ApiOperation("根据id获取商品")
    @PostMapping("getById")
    @LogTag("获取商品")
    public RestResult<ProductDemoResp> getById(@Validated @RequestBody ProductIdReq productIdReq) {
        ProductDemoResp product = productDemoService.getProduct(productIdReq.getId());
        return RestResult.buildSuccess(product);
    }

    @ApiOperation("根据id缓存获取商品")
    @PostMapping("getByIdCache")
    @LogTag("获取商品缓存")
    public RestResult<ProductDemoResp> getByIdCache(@Validated @RequestBody ProductIdReq productIdReq) {
        ProductDemoModel product = redisCacheManager.singleGetData(ProductConstants.PRODUCT_CACHE, productIdReq.getId());
        CtbcAssert.isTrue(product != null, "商品不存在");
        ProductDemoResp resp = new ProductDemoResp();
        BeanUtils.copyProperties(product, resp);
        //直接注入
        staticDataManager.combineModel(resp);
        return RestResult.buildSuccess(resp);
    }

    @ApiOperation("创建商品")
    @PostMapping("createProduct")
    @LogTag("创建商品")
    public RestResult<ProductDemoResp> createProduct(@Validated @RequestBody AddProductReq addProductReq) {
        ProductDemoResp resp = productDemoService.addProduct(addProductReq);
        return RestResult.buildSuccess(resp);
    }

    @ApiOperation("删除商品")
    @LogTag("删除商品")
    @PostMapping("deleteProduct")
    public RestResult<Void> deleteProduct(@Validated @RequestBody ProductIdReq productIdReq) {
        productDemoService.deleteById(productIdReq.getId());
        return RestResult.buildSuccess();
    }

}

